package com.skycong.hello77.util;

import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * 封装http restFull 请求
 *
 * @author ruanmingcong
 * @date 2018/1/9 8:18
 */
public class Http {

    private static final RestTemplate restTemplate = new RestTemplate(true);


    /**
     * request with get
     *
     * @param url     request url
     * @param objects request params
     * @return String
     */
    public static String get(String url, Object... objects) {
        return restTemplate.getForObject(url, String.class, objects);
    }

    /**
     * request with get
     *
     * @param url     request url
     * @param clazz   response type
     * @param objects request params
     * @param <T>
     * @return T
     */
    public static <T> T get(String url, Class<T> clazz, Object... objects) {
        return restTemplate.getForObject(url, clazz, objects);
    }


    /**
     * request with post
     *
     * @param url request url
     * @return String
     */
    public static String post(String url) {
        return post(url, null);
    }

    /**
     * request with post
     *
     * @param url request url
     * @param map request params
     * @return String
     */
    public static String post(String url, MultiValueMap<String, String> map) {
        return post(url, map, String.class);
    }

    /**
     * request with post
     *
     * @param url   request url
     * @param map   request params
     * @param clazz response type
     * @param <T>
     * @return T
     */
    public static <T> T post(String url, MultiValueMap<String, String> map, Class<T> clazz) {
        return restTemplate.postForObject(url, map, clazz);
    }


}
